package com.dm.wallpaper.library.adapters;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.helpers.LocaleHelper;
import com.dm.wallpaper.library.items.Language;
import com.dm.wallpaper.library.preferences.Preferences;
import com.dm.wallpaper.library.utils.Log;
import ohos.agp.components.*;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.util.List;

public class LanguageListProvider extends RecycleItemProvider {

    private List<Language> data;

    private LayoutScatter layoutScatter;

    private Context mContext;

    private CommonDialog commonDialog;


    public LanguageListProvider(Context context, CommonDialog commonDialog) {
        this.mContext = context;
        this.commonDialog = commonDialog;
        this.data = LocaleHelper.getAvailableLanguages(context);
        this.layoutScatter = LayoutScatter.getInstance(context);
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return data != null ? data.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {

        ViewHolder viewHolder = null;
        if (component == null) {
            component = layoutScatter.parse(ResourceTable.Layout_language_itemlayout, null, false);
            viewHolder = new ViewHolder((ComponentContainer) component);
            component.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) component.getTag();
        }
        Language language = data.get(position);
        Log.i("LanguageListProvider.getComponent([position, component, componentContainer]):67       " + language.getName() + "       " + language.getLocale().getCountry());
        viewHolder.languageName.setText(language.getName());

        try {
            if (language.getName().equals(LocaleHelper.getCurrentLanguage(mContext).getName())) {
                viewHolder.languageName.setTextColor(new Color(mContext.getResourceManager().getElement(ResourceTable.Color_green).getColor()));
                VectorElement vectorElement = new VectorElement(mContext, ResourceTable.Graphic_svg_checkbox_checked);
                viewHolder.checked_state.setImageElement(vectorElement);
            } else {
                viewHolder.languageName.setTextColor(new Color(mContext.getResourceManager().getElement(ResourceTable.Color_black).getColor()));
                VectorElement vectorElement = new VectorElement(mContext, ResourceTable.Graphic_svg_checkbox_unchecked);
                viewHolder.checked_state.setImageElement(vectorElement);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        component.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Preferences.get().setCurrentLocale(
                        data.get(position).getLocale().toString());
                LocaleHelper.setLocale(data.get(position).getLocale());
                notifyDataChanged();
                commonDialog.destroy();
            }
        });

        return component;
    }


    static class ViewHolder {

        Text languageName;
        Image checked_state;

        public ViewHolder(ComponentContainer componentContainer) {
            languageName = (Text) componentContainer.findComponentById(ResourceTable.Id_language_name);
            checked_state = (Image) componentContainer.findComponentById(ResourceTable.Id_checked_state);
        }

    }
}
